package com.neusoft.yuejian_6.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian_6.R;
import com.neusoft.yuejian_6.javabean.Activity;
import com.neusoft.yuejian_6.javabean.Apply;

import com.neusoft.yuejian_6.javabean.User;
import com.neusoft.yuejian_6.model.ActivityModel;
import com.neusoft.yuejian_6.model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class ApplyActivity extends AppCompatActivity {
    //声明
    private ImageView left;
    private Button enterAct_button;
    private int user_id;
    private int activity_id;
    private TextView activity_name,activity_price,user_name,user_phone, activity_time,activity_phone,activity_address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply);
        activity_id=getIntent().getIntExtra("activity_id",-1);
        System.out.printf("activity_id");
        initViews();//初始化
        initEvents();//点击事件处理
        getdata();
    }

    private void initViews() {
        activity_name = (TextView) findViewById(R.id.name);

        user_name = (TextView) findViewById(R.id.user_name);
        user_phone = (TextView) findViewById(R.id.user_phone);
        activity_price = (TextView) findViewById(R.id.activity_price);
        activity_time = (TextView) findViewById(R.id.activity_time);
        activity_address = (TextView) findViewById(R.id.activity_address);
        activity_phone = (TextView) findViewById(R.id.activity_phone);
        enterAct_button = (Button) findViewById(R.id.enterAct_button);

        left= (ImageView) findViewById(R.id.left);
    }

    private void initEvents() {
        //返回上一页
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });




        enterAct_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityModel activityModel = new ActivityModel();
                Call<Apply> call = activityModel.addactivityn(user_id,activity_id);
                call.enqueue(new Callback<Apply>() {
                    @Override
                    public void onResponse(Call<Apply> call, Response<Apply> response) {
                        Apply apply=response.body();
                        if (apply.getSuccess().equals("0")) {
                            Toast.makeText(ApplyActivity.this, "失败", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(ApplyActivity.this, "报名成功", Toast.LENGTH_LONG).show();
                        }
                    }
                    @Override
                    public void onFailure(Call<Apply> call, Throwable t) {

                    }
                });
            }
        });



    }

    private void getdata(){
        Bundle bundle = this.getIntent().getExtras();
        activity_id = bundle.getInt("activity_id");
        SharedPreferences sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        user_id = sp.getInt("user_id", 0);

        UserModel userModel = new UserModel();
        Call callu = userModel.getUserInfo(user_id);
        callu.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                User user = response.body();
                user_name.setText(user.getUser_name());
                user_phone.setText(user.getUser_phone());
            }
            @Override
            public void onFailure(Call<User> call, Throwable t) {
            }
        });



        ActivityModel activityModel = new ActivityModel();
        Call<Activity> call = activityModel.getDetail(activity_id);

        call.enqueue(new Callback<Activity>() {
            @Override
            public void onResponse(Call<Activity> call, Response<Activity> response) {
                Activity data = response.body();  //获取数据
                String aname = data.getActivity_name();
                int aprice = data.getActivity_price();
                int activityphone=data.getActivity_phone();
                String aaddress=data.getActivity_address();
                String atime=data.getActivity_time();
                activity_name.setText(aname);
                activity_price.setText("￥"+ aprice+"");
                activity_address.setText(aaddress);
            activity_phone.setText(activityphone+"");
                    activity_time.setText(atime);

            }

            @Override
            public void onFailure(Call<Activity> call, Throwable t) {

            }
        });
    }
}